package com.execdemo.demoorderservice.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Configuration
@EnableAsync
public class ThreadConfig {


    @Bean("myThreadPoolExecutor")
    public ThreadPoolExecutor getThreadPoolExecutor() {
        // 创建一个ThreadPoolExecutor线程池
        // 核心线程数为20，最大线程数为30，空闲线程存活时间为60秒
        // 使用LinkedBlockingQueue作为工作队列，队列大小为10
        // 使用Executors.defaultThreadFactory()创建线程工厂
        // 拒绝执行的处理策略为DiscardOldestPolicy，即丢弃最旧的任务
        return new ThreadPoolExecutor(
                20,
                30,
                60L,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(10),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.DiscardOldestPolicy()
        );
    }


}
